<?php
/**
 * /nmox/modules/Database.class.php
 * Database class
 * 
 * @author David Christian Liedle <david.liedle@gmail.com>
 * @link   http://nmox.org/
 *
 */

class Database {

    var $DB;         // all the info from config
    var $con_handle; // connection handle
    var $queries;    // named queries
    var $results;    // named results
    var $resarray;   // results, put into an array
    
    function Database(){
        // constructor
        global $NMOX_DB;
        $this->DB =& $NMOX_DB;
        if( !$this->DB['kind'] || !$this->DB['host'] || !$this->DB['user'] || !$this->DB['pass'] || !$this->DB['uses'] ){
            die( "Missing NMOX_DB configuration info... Please edit the config file!" );
        }
        
    }

    function connect(){
        // open a database connection
        $this->con_handle = mysql_connect( $this->DB['host'], $this->DB['user'], $this->DB['pass'] )
                             or die( "Could not connect to database... ".mysql_error() );
        $this->select_db();
        return $this->con_handle;
    }
    
    function select_db(){
        mysql_select_db($this->DB['uses'], $this->con_handle)
            or die("Could not select database... ".mysql_error());
    }
    
    function add_query($nickname, $query){
        $this->queries[$nickname] = $query;
    }
    
    function get_query($nickname){
        return $this->queries[$nickname];
    }
    
    function query($query){
        // raw query, one-shot
        $result = mysql_query($query)
                            or die( "Could not complete query: ".mysql_error() );
        return $result;
    }
    
    function do_query($query_name){
        // single query
        $this->results[$query_name] = mysql_query($this->queries[$query_name])
                                        or die( "Could not complete query '".$query_name."': ".mysql_error() );
    }
    
    function do_queries(){
        // all queries
        foreach( $this->queries as $name => $query ){        // name is key, query is value
            $this->results[$name] = $this->query($query);    // now name matches in results
        }
    }

    function get_result($result_name){
        return $this->results[$result_name];
    }
    
    function get_results(){
        return $this->results;
    }
    
    function load_array($from_result_name){
        // load results of queries into useable array:
        while( $row = mysql_fetch_assoc($this->results[$from_result_name]) ){
            $this->resarray[$from_result_name][] = $row;
        }
    }
    
    function get_array($which_array){
        return $this->resarray[$which_array];
    }
    
    function get_arrays(){
        return $this->resarray;
    }
    
    function free_result($result_to_free){
        if( is_resource($result_to_free) ){
            mysql_free_result($result_to_free);
        }
    }
    
    function free_results(){
        // frees all results
        foreach( $this->results as $nickname=>$res ){
            $this->free_result($res);
        }
    }
    
    function close(){
        mysql_close($this->con_handle);
    }

}

locateMe("/nmox/modules/Database.class.php");
